const {
  $util,
} = getApp();
const app = getApp();
const MAX_LENGTH = 15;
Page({
  data: {
    page: 1,
    list: [],
    nav: [{
      title: '待开始',
      id: 1,
    }, {
      title: '进行中',
      id: 2,
    }, {
      title: '已完成',
      id: 3,
    }],
    openType: '', // 初始进入列表时的会议类型
    timetype: 1, // 1-待开始，2-进行中，3-已完成
    hasMore: true,
    loaded: false,
    abnorData: {
      imgSrc: '/common/static/images/abnor-16.png',
      text: '暂无会议',
    },
    isShowFilter: false, // 是否显示类型筛选
    isFilterActive: '', // 类型筛选选中项
    first: true,
    keyword: '', // 搜索关键词
    mode: '', // 1 表示从我的组织页面进入
    addMetting: false, // 管理员才可以添加会议
    imgUrl: $util.getImgUrl(),
  },
  onShow() {
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  onLoad(opts) {
    this.setData(opts);
    this.setData({
      barTitle: decodeURIComponent(opts.barTitle),
    })
    this.loadList();
    this.getinformation();
    wx.setNavigationBarTitle({
      title: opts.barTitle && decodeURIComponent(opts.barTitle) || '会议管理',
    });
  },
  getinformation() { // 添加会议按钮
    $util.request({
      url: 'entry/wxapp/partylife.sessions.getinformation',
      data: {},
      success: (res) => {
        if (res.information && res.information.ismanager == '1') {
          this.setData({
            addMetting: true,
          });
        } else {
          this.setData({
            addMetting: false,
          });
        }
      },
    });
  },
  navigateTo() {
    wx.removeStorageSync('addmeeting');
    wx.removeStorageSync('memberids');
    wx.removeStorageSync('orgids');
    const url = `/ws_party_const/pages/meeting-add/index`;
    app.util.navigateTo(url, {
      openType: this.data.openType,
    });
  },
  onReachBottom() {
    if (!this.data.hasMore) return;
    this.setData({
      page: this.data.page + 1,
    }, () => {
      this.loadList();
    });
  },
  changeNav(e) {
    this.setData({
      page: 1,
      timetype: e.detail.obj.id,
      hasMore: true,
      keyword: '',
    }, () => {
      this.loadList();
    });
  },
  loadList() {
    const {
      page,
      timetype,
      list: d_list,
      first,
      isFilterActive: filter,
      keyword,
      mode, 
      openType,
    } = this.data;
    let param = {
      page,
      timetype,
      type: openType || '',
      keyword: keyword,
      filter: filter,
    };

    $util.request({
      url: mode === '1' ? 'entry/wxapp/partylife.meets.getmeetslist' : 'entry/wxapp/partylife.sessions.getmeeting',
      data: param,
      success: (res) => {
        let {
          list,
          mettype = '',
        } = res;
        if (first) {
          this.setData({
            first: false,
          });
        }
        if (page > 1) {
          list = [...d_list, ...list];
        }
        this.setData({
          mettype: mettype,
          hasMore: res.list.length === MAX_LENGTH,
          list,
          loaded: true,
        });
        wx.setStorageSync('mettype', mettype);
      },
    });
  },
  // 显示类型筛选
  showFilter() {
    this.setData({
      isShowFilter: !this.data.isShowFilter,
    });
  },
  // 切换类型筛选
  changeFilter(e) {
    const { id, idx } = e.currentTarget.dataset;
    const { isFilterActive } = this.data;
    this.setData({
      isFilterActive: isFilterActive === idx ? '' : idx,
      filter: isFilterActive === idx ? '' : id,
      isShowFilter: false,
      page: 1,
    });
    this.loadList();
  },
  bindIpval(e) {
    this.setData({
      keyword: e.detail.value,
    });
  },
  // 搜索
  bindsearch(e) {
    if (e.detail.value.trim() === '') {
      this.setData({
        isFilterActive: '',
        filter: '',
      });
    }
    this.setData({
      page: 1,
    });
    this.loadList();
  },
});